<?php
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph.php';
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph_pie.php';
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph_pie3d.php';

/**
 * JPGraph Pie Chart Wrapper
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class Nina_Graph_Jpgraph_Pie extends Nina_Graph_Jpgraph_Plot
{
	/**
	 * Is this a centred chart?
	 * @access protected
	 * @var boolean
	 */
	protected $centred = false;

	/**
	 * Is this a 3D chart?
	 * @access protected
	 * @var boolean
	 */
	protected $three_d = false;

	/**
	 * Label positioning value (0 = internal, 1 = external)
	 * @access protected
	 * @var string|integer
	 */
	protected $label_position = 'auto';

	/**
	 * Constructor
	 * @param integer [$width=400] The width
	 * @param integer [$height=400] The height
	 * @param boolean [$centred=false] Set a centred pie chart
	 * @param boolean [$three_d=false] Set 3D pie chart
	 */
	public function __construct($width = 400, $height = 400, $centred = false, $three_d = false) {
		$this->graph = new PieGraph($width, $height);
		$this->graph->setMargin(10, 10, 10, 10);

		$this->centred = $centred;
		$this->three_d = $three_d;

		if($centred)
			$this->plot = new PiePlotCExtended(array());
		elseif($three_d)
			$this->plot = new PiePlot3DExtended(array());
		else
			$this->plot = new PiePlotExtended(array());

		parent::__construct();
	}

	/**
	 * Set the perspective angle for a 3D pie
	 * Only works if you have specified a 3D chart in the constructor call
	 * @param integer $angle The angle
	 */
	public function setPerspectiveAngle($angle) {
		if($this->three_d)
			$this->plot->SetAngle($angle);
	}

	/**
	 * Set the title to be put in the centre section
	 * Only works if you have specified a centred chart in the constructor call
	 * @param string $title The title
	 */
	public function setCenterTitle($title) {
		if($this->centred)
			$this->plot->SetMidTitle($title);
	}

	/**
	 * Set the size of the centre section
	 * Only works if you have specified a centred chart in the constructor call
	 * Radius for pie must either be specified as a fraction [0, 0.5] of the size of the
	 * image or as an absolute size in pixels in the range [10, 1000]
	 * @param integer $size The size
	 */
	public function setCenterSize($size) {
		if($this->centred)
			$this->plot->SetMidSize($size);
	}

	/**
	 * Set the color of the centre section
	 * Only works if you have specified a centred chart in the constructor call
	 * @param string $color The color
	 */
	public function setCenterColor($color) {
		if($this->centred)
			$this->plot->SetMidColor($color);
	}

	/**
	 * Set the legends
	 * @param array $legends The legend texts
	 */
	public function setLegend($legends) {
		$this->plot->SetLegends(array_reverse($legends));
	}

	/**
	 * Set the labels
	 * @param array $labels The label texts
	 * @param integer $position The position
	 */
	public function setLabels($labels, $position=null) {
		if(!$position) $position = $this->label_position;
		$this->plot->SetLabels($labels, $position);
	}

	/**
	 * Set the label position offset
	 * @param integer $position The position
	 */
	public function setLabelPosition($position) {
		$this->label_position = $position;
	}

	/**
	 * Set the plot to use numerical value labels
	 */
	public function setValueLabels() {
		$this->plot->SetLabelType(1);
	}

	/**
	 * Set the plot to use percentage labels
	 */
	public function setPercentageLabels() {
		$this->plot->SetLabelType(0);
	}

	/**
	 * Show the border
	 * @param boolean [$outer=true] Show outer border
	 * @param boolean [$inner=true] Show inner border
	 */
	public function showBorder($outer = true, $inner = true) {
		$this->plot->ShowBorder($outer, $inner);
	}

	/**
	 * Set the size of the chart
	 * Radius for pie must either be specified as a fraction [0, 0.5] of the size of the
	 * image or as an absolute size in pixels  in the range [10, 1000]
	 * @param integer|float $size The size
	 */
	public function setSize($size) {
		$this->plot->SetSize($size);
	}

	/**
	 * Set the colors of each slice
	 * @param array $colors The colors
	 */
	public function setSliceColors($colors) {
		$this->plot->SetSliceColors(array_reverse($colors));
	}

	/**
	 * Explode the chart
	 * @param array|integer|null [$explode=null] Array of explode radius values or a single radius value to use for all slices
	 */
	public function setExplode($explode = null) {
		if(is_array($explode))
			$this->plot->Explode($explode);
		elseif(is_numeric($explode))
			$this->plot->ExplodeAll($explode);
		else
			$this->plot->ExplodeAll();
	}

	/**
	 * Explode a single slice
	 * @param integer $slice The index of the slice
	 * @param integer [$radius=20] The explode radius
	 */
	public function explodeSlice($slice, $radius = 20) {
		$this->plot->ExplodeSlice($slice, $radius);
	}

	/**
	 * Remove any labels for zero values then call the main renderer
	 */
	public function render() {
		$labels = $this->plot->getLabels();

		foreach(array_reverse($this->plot->getData()) as $id => $value) {
			if($value == 0)
				$labels[$id] = '';
		}

		$this->setLabels($labels, $this->label_position);
		parent::render();
	}
}

/**
 * JPGraph Pie Chart Extension
 * Adds ability to set data outside of the constructor
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class PiePlotExtended extends PiePlot {

	/**
	 * Set the plot data
	 * @param array $data The data
	 */
	public function setData($data) {
		$this->data = $data;
	}

	/**
	 * Get the plot data
	 * @return array
	 */
	public function getData() {
		return $this->data;
	}

	/**
	 * Get the plot labels
	 * @return array
	 */
	public function getLabels() {
		return $this->labels;
	}
}

/**
 * JPGraph Centred Pie Chart Extension
 * Adds ability to set data outside of the constructor
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class PiePlotCExtended extends PiePlotC {

	/**
	 * Set the plot data
	 * @param array $data The data
	 */
	public function setData($data) {
		$this->data = $data;
	}

	/**
	 * Get the plot data
	 * @return array
	 */
	public function getData() {
		return $this->data;
	}

	/**
	 * Get the plot labels
	 * @return array
	 */
	public function getLabels() {
		return $this->labels;
	}
}

/**
 * JPGraph 3D Pie Chart Extension
 * Adds ability to set data outside of the constructor
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class PiePlot3DExtended extends PiePlot3D {

	/**
	 * Set the plot data
	 * @param array $data The data
	 */
	public function setData($data) {
		$this->data = array_reverse($data);
	}

	/**
	 * Get the plot data
	 * @return array
	 */
	public function getData() {
		return $this->data;
	}

	/**
	 * Get the plot labels
	 * @return array
	 */
	public function getLabels() {
		return $this->labels;
	}
}
